//订单列表实例
var $table = $("#comment_list_table");


$(function () {
    initTable($table);
//鼠标经过预览图片
    imagePreview();
});

this.imagePreview = function () {
    var xOffset = 10;
    var yOffset = 30;
    $("#comment_list_table").on("mouseover", "img", function (e) {
        // alert(1);
        var imgsrc = $(this).attr("src");
        $("body").append("<p id='preview'><img src='" + imgsrc + "' alt='Image preview' /></p>");
        $("#preview").css("top", (e.pageY - xOffset - 150) + "px").css("left", (e.pageX - yOffset + 50) + "px").fadeIn("fast");
    });
    $("#comment_list_table").on("mouseout", "img", function (e) {
        $("#preview").remove();
    });
};

var columns = [
    {
        title: "序号",
        align: "center",
        valign: "middle",
        width: "50px",
        formatter: function (value, row, index) {
            return (index + 1);
        }
    }, {
        field: "id",
        title: "评论ID",
        halign: "center",
        valign: "middle",
        align: "center",
        width: "50px"
    }, {
        field: "goods",
        title: "评价商品",
        valign: "middle",
        halign: "center",
        width: "250px",
        sortable: true,
        formatter: function (value, row, index) {
            var liHtml = "";
            liHtml += "    <tr class=\"content-row\">\n" +
                "<td><img style=\"width: 50px;height: 50px;\" src=\"" + value.goodsUrl + "\"></td>" +
                "<td>\n" +
                "<div style=\"width: 150px;font-size: 12px;\"><a href=\"javascript:;\">" +
                value.goodsName + "</a>" +
                "</div>\n" +
                "</td>\n" +
                "</tr>";
            var htmlStr =
                "                            <table class=\"table order-list\">\n" +
                "                                <tbody id=\"modalGoodsList\" class=\"widget-list-item\">\n" +
                liHtml +
                "                                </tbody>\n" +
                "                            </table>";
            return htmlStr;
        }
    }, {
        field: "content",
        title: "评论内容",
        halign: "center",
        valign: "middle",
        width: "400px",
        sortable: true,
        formatter: function (value, row, index) {
            value = (value == null ? "" : value);
            var html = "<div style='font-size: 12px'>" + value + "</div>";
            var picArr = [];
            var htmlStr = "";
            if (row.commentPic != null) {
                picArr = row.commentPic.split(",");
                for (var zz of picArr) {
                    htmlStr += '<img style="width: 50px;height: auto" src="' + zz + '" >';
                }
            }
            html += "<div>" + htmlStr + "</div>";
            return html;
        }
    }, {
        field: "muser",
        title: "评论人",
        align: "center",
        valign: "middle",
        width: "70px",
        formatter: function (value, row, index) {
            return value.userName;
        }
    }, {
        field: "muser",
        title: "联系方式",
        align: "center",
        valign: "middle",
        width: "70px",
        formatter: function (value, row, index) {
            return value.phone;
        }
    }, {
        field: "gmtDatetime",
        title: "评论时间",
        align: "center",
        valign: "middle",
        halign: "center",
        width: "80px",
        sortable: true
    }, {
        title: "操作",
        halign: "center",
        valign: "middle",
        width: "150px",
        sortable: true,
        formatter: function (value, row, index) {
            var optHtml = "";
            optHtml += "<a class=\"btn btn-danger btn-xs\" href=\"javascript:;\" onclick=\"doDelete(" + row.id + ")\"> 删除</a>\n";
            return optHtml;
        }
    }];

var queryParams = function (params) {
    console.log("params:", params);
    var search = params.search == undefined ? "" : params.search;
    //封装page，搜索筛选参数放到condition中
    var pageParam = {
        offset: params.offset,
        current: params.offset / params.limit + 1,//bootstrap-table分页参数offset从0开始，计算的后端当前页参数current=(offset/limit+1)，
        size: params.limit,
        condition: {
            searchText: search
        }
    };

    return pageParam;
}


//初始化表格 tableins:表格实例 ,
function initTable(tableins) {
    tableins.bootstrapTable({
        columns: columns,
        method: "get",
        url: urlcore + "/api/evaluate/getCommentsAdmin",
        dataType: "json",
        queryParams: function (params) {
            return queryParams(params);
        },
        //返回数据预处理
        responseHandler: function (cbdata) {
            console.log(cbdata);
            if (null != cbdata.data && null != cbdata.data.records) {
                var records = cbdata.data.records;
                console.log(records);
                var res = {
                    //如果采用server端分页，必须有rows和total这两部分数据
                    "rows": records,
                    "total": cbdata.data.total
                };
                return res;
            } else return {"rows": [], "total": 0};
        },
        cache: false,
        uniqueId: "id",
        // toolbar: '#OrderListToolbar',//自定义工具栏，不同表格工具栏不同，一个jQuery 选择器，指明自定义的toolbar 例如:#toolbar, .toolbar.
        pagination: true,//启用分页条
        sidePagination: "server",//分页方式client或server
        // pageNumber: 1,
        pageSize: 16,//每页大小
        pageList: [16, 32, 48, 64],//可以选择每页大小
        showRefresh: true,//显示刷新
        sortable: true,//是否启用排序
        // clickToSelect: true,//设置true 将在点击行时，自动选择rediobox 和 checkbox
        search: true,//默认为客户端搜索，服务器端接收params中的searchText后可以做服务器端搜索
        // searchText:"",//初始搜索文本
    });
}

//刪除
function doDelete(id) {
    confirm("确认删除该评论吗？", function (callback) {
        if (callback) {
            delGoods();
        }
    });

    function delGoods() {
        $.ajax({
            type: "GET",
            url: urlcore + "/api/evaluate/deleteComment?id=" + id,
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: {},
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    $table.bootstrapTable("refresh");
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("删除失败");
            }
        });
    }

}
